<?php
/***************************************************************************
 *                                common.php
 *                            -------------------
 *                      (APU) Automated Pilot Utility
 *   begin                : Tuesday, Nov 9, 2004
 *   copyright            : (C) 2004-2006 SF Games
 *   email                : techsupport@sf-games.com
 ***************************************************************************/

include_once('commondefs.php');

/***************************************************************************
 *   APU General purpose functions
 ***************************************************************************/

//
// Security measure, to ensure SQL query parameters are properly escaped.
//
function sqlfix($s)
{
	// mysql_escape_string is deprecated
	// use mysql_real_escape_string for PHP 4 >= 4.3.0, PHP 5

//	sPHPVersion = $PHP_VERSION; //phpversion();
//	phpVersion  = explode(".", sPHPVersion);

//	if ( phpVersion[0] > 4 || (phpVersion[0] >= 4 && phpVersion[1] >= 3 && phpVersion[2] >= 0) )
//		return "'".mysql_real_escape_string($s)."'";

	return "'".mysql_escape_string($s)."'";
}

//
// Returns a '\r\n' for bSpecial == false
// Returns a '\n'   for bSpecial == true
// FreeBSD, and apparently Windows servers sending e-mail headers needs non standard \n
//
function getEmailCRLF($bSpecial)
{
	$sCRLF = "\r\n";

	if ( $bSpecial == true )
		$sCRLF = "\n";

	return $sCRLF;
}

//
// Returns today as string in MySQL date string YYYY-MM-DD
//
function getTodayAsSQLDate()
{
	$today        = getdate();
	$todaySQLDate = sprintf("%04d-%02d-%02d", $today["year"], $today["mon"], $today["mday"]);
	return $todaySQLDate;
}

//
// Returns today as string in US date format MM-DD-YYYY
//
function getTodayAsUSDate()
{
	$today        = getdate();
	$todayUSDate = sprintf("%02d-%02d-%04d", $today["mon"], $today["mday"], $today["year"]);
	return $todayUSDate;
}

//
// Given a SQL formatted date string YYYY-MM-DD
// Return a US date string of MM-DD-YYYY
//
function SQLDateToUSDate($sDate)
{
	$sDay   = "";
	$sMonth = "";
	$sYear  = "";
	$retVal = "";

	//
	// Seperate out the month day and year
	//

	// Get Year
	$tok = strtok($sDate, "/-\n");
	if ($tok)
		$sYear = $tok;

	// Get month
	$tok = strtok("/-\n");
	if ($tok)
		$sMonth = $tok;

	// Get day
	$tok = strtok("/-\n");
	if ($tok)
		$sDay = $tok;

	// Convert to US date format of MM-DD-YYYY
	$retVal = sprintf("%s-%s-%s", $sMonth, $sDay, $sYear);

	return $retVal;
}

//
// Given a SQL formatted date string YYYY-MM-DD
// Return a US date string of Month Day, YYYY
// Example: 2006-11-12 to November 12, 2006
//
function SQLDateToShortUSDate($sDate)
{
	global $monthsArrayFull;
	$iDay   = "";
	$iMonth = "";
	$sYear  = "";
	$retVal = "";

	//
	// Seperate out the month day and year
	//

	// Get Year
	$tok = strtok($sDate, "/-\n");
	if ($tok)
		$sYear = $tok;

	// Get month
	$tok = strtok("/-\n");
	if ($tok)
		$iMonth = intval($tok);

	// Get day
	$tok = strtok("/-\n");
	if ($tok)
		$iDay = intval($tok);

	// Convert to US date format of MM-DD-YYYY
	$retVal = sprintf("%s %d, %s", $monthsArrayFull[$iMonth], $iDay, $sYear);

	return $retVal;
}


//
// Given a SQL formatted date string YYYY-MM-DD
// Return a Euro date string of DD-MM-YYYY
//
function SQLDateToEuroDate($sDate)
{
	$sDay   = "";
	$sMonth = "";
	$sYear  = "";
	$retVal = "";

	//
	// Seperate out the month day and year
	//

	// Get Year
	$tok = strtok($sDate, "/-\n");
	if ($tok)
		$sYear = $tok;

	// Get month
	$tok = strtok("/-\n");
	if ($tok)
		$sMonth = $tok;

	// Get day
	$tok = strtok("/-\n");
	if ($tok)
		$sDay = $tok;

	// Convert to Euro date format of DD-MM-YYYY
	$retVal = sprintf("%s-%s-%s", $sDay, $sMonth, $sYear);

	return $retVal;
}

//
// Given a SQL formatted date string YYYY-MM-DD
// Return a Euro date string of Day Month YYYY
// Example: 2006-11-12 to 12 November 2006
//
function SQLDateToShortEuroDate($sDate)
{
	global $monthsArrayFull;
	$iDay   = "";
	$iMonth = "";
	$sYear  = "";
	$retVal = "";

	//
	// Seperate out the month day and year
	//

	// Get Year
	$tok = strtok($sDate, "/-\n");
	if ($tok)
		$sYear = $tok;

	// Get month
	$tok = strtok("/-\n");
	if ($tok)
		$iMonth = intval($tok);

	// Get day
	$tok = strtok("/-\n");
	if ($tok)
		$iDay = intval($tok);

	// Convert to US date format of MM-DD-YYYY
	$retVal = sprintf("%d %s, %s", $iDay, $monthsArrayFull[$iMonth], $sYear);

	return $retVal;
}

//
// Returns current server time as format HH:MM:SS
//
function getCurrentTime()
{
	$today     = getdate();
	$todayTime = sprintf("%02d:%02d:%02d", $today["hours"], $today["minutes"], $today["seconds"]);
	return $todayTime;
}

//
// Returns the current year as a string
// example: "2005"
//
function getCurrentYear()
{
	$today = getdate();
	return $today["year"];
}

//
// Returns the current short month name, given a number 1-12
//
function getShortMonthName($nMonth)
{
	// Declare an array of max days in months
	global $monthsArrayShort;

	$retVal = $monthsArrayShort[$nMonth];
	return $retVal;
}

//
// Returns the current long month name, given a number 1-12
//
function getLongMonthName($nMonth)
{
	// Declare an array of max days in months
	global $monthsArrayFull;

	$retVal = $monthsArrayFull[$nMonth];
	return $retVal;
}

//
// Function to validate a given date string
// Date strings expected can be of the following forms
// US:
// 		MM/DD/YYYY
// 		MM/DD/YY
// 		MM-DD-YYYY
// 		MM-DD-YY
//
// Euro
// 		DD/MM/YYYY
// 		DD/MM/YY
// 		DD-MM-YYYY
// 		DD-MM-YY
function verifyDate($sDate, $bEuroFormat)
{
	// Declare an array of max days in months
	global $monthsArrayNum;
	$retVal  = true;
	$sDay    = "";
	$sMonth  = "";
	$sYear   = "";

	//
	// Seperate out the month day and year
	//

	if ( $bEuroFormat )
	{
		// Get day
		$tok = strtok($sDate, "/-\n");
		if ($tok)
			$sDay = $tok;

		// Get month
		$tok = strtok("/-\n");
		if ($tok)
			$sMonth = $tok;
	}
	else
	{
		// Get month
		$tok = strtok($sDate, "/-\n");
		if ($tok)
			$sMonth = $tok;

		// Get day
		$tok = strtok("/-\n");
		if ($tok)
			$sDay = $tok;
	}

	// Get Year
	$tok = strtok("/-\n");
	if ($tok)
		$sYear = $tok;

	// Promote 2 digit year to 4 digit
	// Good up to the year 2099
	if ( strlen($sYear) == 2 )
		$sYear = $sYear + 2000;

	// Valid year
	if ( $sYear < 2004 || $sYear > 2099 )
	{
		$retVal = false;
		return $retVal;
	}

	// Valid month
	if ( intval($sMonth) < 1 || intval($sMonth) > 12 )
	{
		$retVal = false;
		return $retVal;
	}

	// Valid day
	if ( intval($sDay) < 1 || intval($sDay) > 31 )
	{
		$retVal = false;
		return $retVal;
	}

	if ( intval($sDay) > $monthsArrayNum[intval($sMonth)] )
	{
		$retVal = false;
		return $retVal;
	}

	return $retVal;
}

//
// Function to convert a date to MySQL date format YYYY-MM-DD
// Expects on input MM-DD-YYYY MM/DD/YYYY MM-DD-YY or MM/DD/YY for US format
// Expects on input DD-MM-YYYY DD/MM/YYYY DD-MM-YY or DD/MM/YY for Euro format
//
function dateToMySQL($sDate, $bEuroFormat)
{
	global $retVal;
	$sDay    = "";
	$sMonth  = "";
	$sYear   = "";

	$retVal = TRUE;

	//
	// Seperate out the month day and year
	//

	if ( $bEuroFormat )
	{
		// Get day
		$tok = strtok($sDate, "/-\n");
		if ($tok)
			$sDay = $tok;

		// Get month
		$tok = strtok("/-\n");
		if ($tok)
			$sMonth = $tok;
	}
	else
	{
		// Get month
		$tok = strtok($sDate, "/-\n");
		if ($tok)
			$sMonth = $tok;

		// Get day
		$tok = strtok("/-\n");
		if ($tok)
			$sDay = $tok;
	}

	// Get Year
	$tok = strtok("/-\n");
	if ($tok)
		$sYear = $tok;

	// Promote 2 digit year to 4 digit
	// Good up to the year 2099
	if ( strlen($sYear) == 2 )
		$sYear = $sYear + 2000;

	// Convert to mySQL date format of YYYY-MM-DD
	$retVal = sprintf("%s-%s-%s", $sYear, $sMonth, $sDay);

	return $retVal;
}

// Formats and returns a SQL style date representing the beginning of the current month
// Example: '2004-11-01'
function getBegMonth()
{
	$today = getdate();
	$retVal = sprintf("%04d-%02d-01", $today["year"], $today["mon"]);
	return $retVal;
}

// Formats and returns a SQL style date representing the beginning of the given month and year
// Example: '2004-11-01'
function getBegGivenMonth($month, $year)
{
	$retVal = sprintf("%04d-%02d-01", $year, $month);
	return $retVal;
}

// Formats and returns a SQL style date representing the end of the current month
// Example: '2004-11-30'
function getEndMonth()
{
	// Declare an array of max days in months
	global $monthsArrayNum;

	$today = getdate();
	$retVal = sprintf("%04d-%02d-%02d", $today["year"], $today["mon"], $monthsArrayNum[$today["mon"]]);
	return $retVal;
}

// Formats and returns a SQL style date representing the end of the current month
// Example: '2004-11-30'
function getEndGivenMonth($month, $year)
{
	// Declare an array of max days in months
	global $monthsArrayNum;

	$retVal = sprintf("%04d-%02d-%02d", $year, $month, $monthsArrayNum[$month]);
	return $retVal;
}

// Formats and returns a SQL style date representing the beginning of last month
// Example: '2004-11-01'
function getBegLastMonth()
{
	$today = getdate();

	if ( $today["mon"] == 1 )
		$retVal = sprintf("%04d-12-01", $today["year"]-1);
	else
	{
		$retVal = sprintf("%04d-%02d-01", $today["year"], $today["mon"]-1);
	}

	return $retVal;
}

// Formats and returns a SQL style date representing the beginning of last month from given month and year
// Example: '2004-11-01'
function getBegLastGivenMonth($month, $year)
{
	if ( $month == 1 )
		$retVal = sprintf("%04d-12-01", $year-1);
	else
	{
		$retVal = sprintf("%04d-%02d-01", $year, $month-1);
	}

	return $retVal;
}

// Formats and returns a SQL style date representing the end of last month
// Example: '2004-11-30'
function getEndLastMonth()
{
	// Declare an array of max days in months
	global $monthsArrayNum;

	$today = getdate();

	if ( $today["mon"] == 1 )
		$retVal = sprintf("%04d-12-31", $today["year"]-1);
	else
	{
		$retVal = sprintf("%04d-%02d-%02d", $today["year"], $today["mon"]-1, $monthsArrayNum[$today["mon"]-1]);
	}

	return $retVal;
}

// Formats and returns a SQL style date representing the end of last month given month and year
// Example: '2004-11-30'
function getEndLastGivenMonth($month, $year)
{
	// Declare an array of max days in months
	global $monthsArrayNum;

	if ( $month == 1 )
		$retVal = sprintf("%04d-12-31", $year-1);
	else
	{
		$retVal = sprintf("%04d-%02d-%02d", $year, $month-1, $monthsArrayNum[$month-1]);
	}

	return $retVal;
}

// Formats and returns a SQL style date representing the beginning of the current year
// Example: '2004-01-01'
function getBegYear()
{
	$today = getdate();
	$retVal = sprintf("%04d-01-01", $today["year"]);
	return $retVal;
}

// Formats and returns a SQL style date representing the beginning of the given year
// Example: '2004-01-01'
function getBegGivenYear($year)
{
	$retVal = sprintf("%04d-01-01", $year);
	return $retVal;
}

// Returns the 3 char current month
function getShortMonth()
{
	// Declare an array of max days in months
	global $monthsArrayShort;

	$today = getdate();
	$retVal = $monthsArrayShort[$today["mon"]];
	return $retVal;
}

// Returns the 3 char last month
function getLastShortMonth()
{
	// Declare an array of max days in months
	global $monthsArrayShort;

	$today = getdate();

	if ( $today["mon"] == 1 )
		$retVal = "Dec";
	else
		$retVal = $monthsArrayShort[$today["mon"]-1];

	return $retVal;
}


// Given a date, returns true if the next day is next year
// i.e if date given is 12-31-2004
function isTomorrowNextYear($endDay, $endMonth, $endYear)
{
	$retVal = false;

	if ( $endDay == 31 && $endMonth == 12 )
		$retVal = true;

	return $retVal;
}

// Given a date, returns true if the next day is next month
function isTomorrowNextMonth($endDay, $endMonth, $endYear)
{
	// Declare an array of max days in months
	global $monthsArrayNum;
	$retVal  = false;
	$tempDay = $endDay;
	$tempDay++;

	// If tomorrow is past the last day of the given month, then it will be next month
	if ( $tempDay > $monthsArrayNum[$endMonth] )
		$retVal = true;

	return $retVal;
}

// Returns an array of numeric values representing days, hours, minutes & seconds respectively
function date_diff($earlierDate, $laterDate)
{
  $ret = array('days'=>0, 'hours'=>0, 'minutes'=>0, 'seconds'=>0);

  $totalsec = $laterDate - $earlierDate;

  if ($totalsec >= 86400)
  {
	$ret['days'] = floor($totalsec / 86400);
	$totalsec = $totalsec % 86400;
  }

  if ($totalsec >= 3600)
  {
   $ret['hours'] = floor($totalsec / 3600);
   $totalsec = $totalsec % 3600;
  }

  if ($totalsec >= 60)
  {
   $ret['minutes'] = floor($totalsec / 60);
  }

  $ret['seconds'] = $totalsec % 60;

  return $ret;
}

//
// Send e-mail
//
function sendEmail($title, $to, $subject, $message)
{
	include('config.php');
	include('commondefs.php');
	$combinedMessage = "";

	// Headers to make this an HTML e-mail
	$headers  = "MIME-Version: 1.0" .getEmailCRLF($bSpecialCRLF);
	$headers  = "Content-type: text/html; charset=iso-8859-1" .getEmailCRLF($bSpecialCRLF);
	$headers .= "From: " .$emailFromFriendly ."<" .$emailFromAddress .">" .getEmailCRLF($bSpecialCRLF);

	// Prettify an HTML e-mail
	$combinedMessage  = "<html>\n";
	$combinedMessage .= "<head>\n";
	$combinedMessage .= "<title>" .$title ."</title>\n";
	$combinedMessage .= "<meta http-equiv=\"Content-Type\" content=\"text/html;\">\n";
	$combinedMessage .= "<style>\n";
	$combinedMessage .= "<!--\n";
	$combinedMessage .= "body { background-image: url(img/casky2.jpg); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; color: #000000; background-color: transparent; }\n";
	$combinedMessage .= "a:link { font-family: Verdana, Arial, Helvetica, sans-serif; text-decoration: none; color: #990000; background-color: transparent; font-weight: bold; }\n";
	$combinedMessage .= "a:hover { font-family: Verdana, Arial, Helvetica, sans-serif; color: #3366CC; background-color: transparent; text-decoration: none; font-weight: bold; }\n";
	$combinedMessage .= "a:active { 	font-family: Verdana, Arial, Helvetica, sans-serif; color: #990000; background-color: transparent; text-decoration: none; font-weight: bold; }\n";
	$combinedMessage .= "a:visited { font-family: Verdana, Arial, Helvetica, sans-serif; text-decoration: none; font-weight: bold; }\n";
	$combinedMessage .= ".textdark { font-family: Verdana, Arial, Helvetica, sans-serif; color: #003399; background-color: transparent; font-size: 10px; font-weight: bold; }\n";
	$combinedMessage .= ".textdarkCenter { font-family: Verdana, Arial, Helvetica, sans-serif; color: #003399; background-color: transparent; font-size: 10px; font-weight: bold; text-align: center; }\n";
	$combinedMessage .= ".textdarkRight { font-family: Verdana, Arial, Helvetica, sans-serif; color: #003399; background-color: transparent; font-size: 10px; font-weight: bold; text-align: right; }\n";
	$combinedMessage .= ".textdarkB { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; font-weight: bold; color: #003399; background-color: transparent; }\n";
	$combinedMessage .= ".textred { font-family: Verdana, Arial, Helvetica, sans-serif; color: #990000; background-color: transparent; font-size: 10px; font-weight: bold; }\n";
	$combinedMessage .= ".textredCenter { font-family: Verdana, Arial, Helvetica, sans-serif; color: #990000; background-color: transparent; font-size: 10px; font-weight: bold; text-align: center; }\n";
	$combinedMessage .= ".textredRight { font-family: Verdana, Arial, Helvetica, sans-serif; color: #990000; background-color: transparent; font-size: 10px; font-weight: bold; text-align: right; }\n";
	$combinedMessage .= ".textredB { font-family: Verdana, Arial, Helvetica, sans-serif; color: #990000; background-color: transparent; font-size: 12px; font-weight: bold; }\n";
	$combinedMessage .= ".textredBLrg { font-family: Verdana, Arial, Helvetica, sans-serif; color: #990000; background-color: transparent; font-size: 18px; font-weight: bold; }\n";
	$combinedMessage .= ".editfield { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; color: #000000; background-color: #FFFFCC; border: thin solid #7D6D4C; }\n";
	$combinedMessage .= ".table_menu { border: 0px none #999999; border-collapse: collapse; width: 25%; margin-left: auto; margin-right: auto; background: #FFFFFF; }\n";
	$combinedMessage .= ".table_roster { border: 0px none #999999; border-collapse: collapse; width: 90%; margin-left: auto; margin-right: auto; background: #FFFFFF; }\n";
	$combinedMessage .= ".table_loa { border: 0px none #999999; border-collapse: collapse; width: 55%; margin-left: auto; margin-right: auto; background: #FFFFFF; }\n";
	$combinedMessage .= ".table_pirep { border: 0px none #999999; border-collapse: collapse; width: 75%; margin-left: auto; margin-right: auto; background: #FFFFFF; }\n";
	$combinedMessage .= ".table_pilot_record { border: 0px none #999999; border-collapse: collapse; width: 375px; margin-left: auto; margin-right: auto; background: #FFFFFF; }\n";
	$combinedMessage .= ".table_roster_header { border: 0px none #999999; border-collapse: collapse; width: 30%; margin-left: auto; margin-right: auto; background: #FFFFFF; }\n";
	$combinedMessage .= ".table_roster_footer { border: 0px none #999999; border-collapse: collapse; width: 50%; margin-left: auto; margin-right: auto; background: #FFFFFF; }\n";
	$combinedMessage .= "th { padding: 4px 4px; border: 0px solid #CCCCCC; }\n";
	$combinedMessage .= "td { text-align: left; padding: 4px 4px; border-top: 1px solid #999999; border-right: 1px solid #999999; border-bottom: 1px none #999999; border-left: 1px solid #999999; vertical-align: middle; }\n";
	$combinedMessage .= ".tableTextLeft { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; text-align: left; color: black; background-color: transparent; }\n";
	$combinedMessage .= ".tableTextRight { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; text-align: right; color: black; background-color: transparent; }\n";
	$combinedMessage .= ".tableTextCenterRedB { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; font-weight: bold; text-align: center; color: #990000; background-color: transparent; }\n";
	$combinedMessage .= ".tableTextRightRedB { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; font-weight: bold; text-align: right; color: #990000; background-color: transparent; }\n";
	$combinedMessage .= ".tableTextCenter { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; text-align: center; color: black; background-color: transparent; }\n";
	$combinedMessage .= ".tableTextCenterB { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; text-align: center; color: black; font-weight: bold; background-color: transparent; }\n";
	$combinedMessage .= "caption { font-family:Verdana, Arial, Helvetica, sans-serif; color: #990000; font-size: 12px; font-weight: bold; caption-side: top; padding-bottom: 1.0em; padding-top: 1.0em; margin-left: auto; margin-right: auto; }\n";
	$combinedMessage .= "thead tr th { vertical-align: bottom; color: white; background: #3366CC; font-family: Verdana, Arial, Helvetica, sans-serif; font-size:12px; }\n";
	$combinedMessage .= "tbody tr td { text-align: left; padding-right: 0.5em; color: #000000; background: transparent; font-family: Verdana, Arial, Helvetica, sans-serif; font-size:11px; }\n";
	$combinedMessage .= "tfoot tr td { color: black; background: #CCCCCC; font-family: Verdana, Arial, Helvetica, sans-serif; font-size:12px; }\n";
	$combinedMessage .= "-->\n";
	$combinedMessage .= "</style>\n";
	$combinedMessage .= "</head>\n";
	$combinedMessage .= "<body>\n";

	// Senders message
	$combinedMessage .= $message;

	// End Prettify of an HTML e-mail
	$combinedMessage .= "<div align=\"center\"><br /><p>" .$APU_COPYRIGHT ."</p></div>\n";
	$combinedMessage .= "</body>\n";
	$combinedMessage .= "</html>\n";

	return mail($to, $subject, $combinedMessage, $headers);
}

?>
